package code.example.base.exception.oom.stack;

/**
 * Created by lei.yan on 2017/6/26.
 *   java.lang.StackOverflowError异常：
 *     线程默认栈内存有1M（可用xss参数指定），如果线程执行时栈内存大于1M，会报 StackOverflowError异常
 *     导至异常的原因：
 *       1. 线程中方法的调用深度 过大，可造成StackOverflowError异常（每个方法调用时，都会压入栈，保存方法的相关信息）
 *       2. 线程中方法的局部变量过多，可造成StackOverflowError异常（栈内会保存被调用方法的局部变量）。
 *
 *
 */
public class TestStackOverFlowError {
    int count =0;
    /**
     * 栈内帧中局部变量表，0个变量。
     *  递归调用9000多次出现：StackOverflowError异常
     */
    public void testStackOverFlowError(){
           System.out.println(++count);
        testStackOverFlowError();
        System.out.println(" ");
    }


    /**
     * 栈内帧中局部变量表，100个变量。
     * 递归调用500多次出现：StackOverflowError异常
     */
    public void testStackOverFlowErrorUseManyVar(double p){
        double	x1	=	p	+	1	;
        double	x2	=	p	+	2	;
        double	x3	=	p	+	3	;
        double	x4	=	p	+	4	;
        double	x5	=	p	+	5	;
        double	x6	=	p	+	6	;
        double	x7	=	p	+	7	;
        double	x8	=	p	+	8	;
        double	x9	=	p	+	9	;
        double	x10	=	p	+	10	;
        double	x11	=	p	+	11	;
        double	x12	=	p	+	12	;
        double	x13	=	p	+	13	;
        double	x14	=	p	+	14	;
        double	x15	=	p	+	15	;
        double	x16	=	p	+	16	;
        double	x17	=	p	+	17	;
        double	x18	=	p	+	18	;
        double	x19	=	p	+	19	;
        double	x20	=	p	+	20	;
        double	x21	=	p	+	21	;
        double	x22	=	p	+	22	;
        double	x23	=	p	+	23	;
        double	x24	=	p	+	24	;
        double	x25	=	p	+	25	;
        double	x26	=	p	+	26	;
        double	x27	=	p	+	27	;
        double	x28	=	p	+	28	;
        double	x29	=	p	+	29	;
        double	x30	=	p	+	30	;
        double	x31	=	p	+	31	;
        double	x32	=	p	+	32	;
        double	x33	=	p	+	33	;
        double	x34	=	p	+	34	;
        double	x35	=	p	+	35	;
        double	x36	=	p	+	36	;
        double	x37	=	p	+	37	;
        double	x38	=	p	+	38	;
        double	x39	=	p	+	39	;
        double	x40	=	p	+	40	;
        double	x41	=	p	+	41	;
        double	x42	=	p	+	42	;
        double	x43	=	p	+	43	;
        double	x44	=	p	+	44	;
        double	x45	=	p	+	45	;
        double	x46	=	p	+	46	;
        double	x47	=	p	+	47	;
        double	x48	=	p	+	48	;
        double	x49	=	p	+	49	;
        double	x50	=	p	+	50	;
        double	x51	=	p	+	51	;
        double	x52	=	p	+	52	;
        double	x53	=	p	+	53	;
        double	x54	=	p	+	54	;
        double	x55	=	p	+	55	;
        double	x56	=	p	+	56	;
        double	x57	=	p	+	57	;
        double	x58	=	p	+	58	;
        double	x59	=	p	+	59	;
        double	x60	=	p	+	60	;
        double	x61	=	p	+	61	;
        double	x62	=	p	+	62	;
        double	x63	=	p	+	63	;
        double	x64	=	p	+	64	;
        double	x65	=	p	+	65	;
        double	x66	=	p	+	66	;
        double	x67	=	p	+	67	;
        double	x68	=	p	+	68	;
        double	x69	=	p	+	69	;
        double	x70	=	p	+	70	;
        double	x71	=	p	+	71	;
        double	x72	=	p	+	72	;
        double	x73	=	p	+	73	;
        double	x74	=	p	+	74	;
        double	x75	=	p	+	75	;
        double	x76	=	p	+	76	;
        double	x77	=	p	+	77	;
        double	x78	=	p	+	78	;
        double	x79	=	p	+	79	;
        double	x80	=	p	+	80	;
        double	x81	=	p	+	81	;
        double	x82	=	p	+	82	;
        double	x83	=	p	+	83	;
        double	x84	=	p	+	84	;
        double	x85	=	p	+	85	;
        double	x86	=	p	+	86	;
        double	x87	=	p	+	87	;
        double	x88	=	p	+	88	;
        double	x89	=	p	+	89	;
        double	x90	=	p	+	90	;
        double	x91	=	p	+	91	;
        double	x92	=	p	+	92	;
        double	x93	=	p	+	93	;
        double	x94	=	p	+	94	;
        double	x95	=	p	+	95	;
        double	x96	=	p	+	96	;
        double	x97	=	p	+	97	;
        double	x98	=	p	+	98	;
        double	x99	=	p	+	99	;
        double	x100	=	p	+	100	;



        System.out.println(++count);
        double  sumValue =  x1    + x2    + x3 + x4    + x5    + x6    + x7    + x8    + x9    + x10   + x11   + x12   + x13   + x14   + x15   + x16   + x17   + x18   + x19   + x20   + x21   + x22   + x23   + x24   + x25   + x26   + x27   + x28   + x29   + x30   + x31   + x32   + x33   + x34   + x35   + x36   + x37   + x38   + x39   + x40   + x41   + x42   + x43   + x44   + x45   + x46   + x47   + x48   + x49   + x50   + x51   + x52   + x53   + x54   + x55   + x56   + x57   + x58   + x59   + x60   + x61   + x62   + x63   + x64   + x65   + x66   + x67   + x68   + x69   + x70   + x71   + x72   + x73   + x74   + x75   + x76   + x77   + x78   + x79   + x80   + x81   + x82   + x83   + x84   + x85   + x86   + x87   + x88   + x89   + x90   + x91   + x92   + x93   + x94   + x95   + x96   + x97   + x98   + x99   + x100 ;
        double newP =  sumValue/1000;
        testStackOverFlowErrorUseManyVar(newP);

        System.out.println(p);
    }

    public void testStackOverFlowErrorUseManyVar(){
        testStackOverFlowErrorUseManyVar(1.0);
    }
    public static  void main(String args []){
        try {
            TestStackOverFlowError test = new TestStackOverFlowError();
            //test.testStackOverFlowError();
            test.testStackOverFlowErrorUseManyVar();
        }catch (Throwable e){

             System.out.println(e.getClass().getName());
        }
    }
}
